package com.aef.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;


public class IsNumberUtils {

	public static boolean isNumeric(String str){ 
		Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+"); 
		Matcher isNum = pattern.matcher(str);
		if( !isNum.matches() ){
			return false; 
		} 
		return true; 
	}
	/** 
	 * 生成随机字符串 
	 * @return 
	 */  
	public static String createNonceStr() {  
		return UUID.randomUUID().toString().toUpperCase().replace("-", "");  
	}  
	/** 
	 * 生成商户订单号 
	 * @param mch_id  商户号 
	 * @param userId  该用户的userID 
	 * @return 
	 */  
	public static String createBillNo(String MCH_ID){  
		//组成： mch_id+yyyymmdd+10位一天内不能重复的数字  
		//10位一天内不能重复的数字实现方法如下:  
		//因为每个用户绑定了userId,他们的userId不同,加上随机生成的(10-length(userId))可保证这10位数字不一样  
		Date dt=new Date();  
		SimpleDateFormat df = new SimpleDateFormat("yyyymmdd");  
		String nowTime= df.format(dt);  
		int length = 10 - MCH_ID.length();  
		return MCH_ID + nowTime + MCH_ID + getRandomNum(length);  
	}  
	/** 
	 * 生成特定位数的随机数字 
	 * @param length 
	 * @return 
	 */  
	public static String getRandomNum(int length) {  
		String val = "";  
		Random random = new Random();  
		for (int i = 0; i < length; i++) {  
			val += String.valueOf(random.nextInt(10));  
		}  
		return val;  

	} 
	/**
	 * xml转为json
	 * @param xml
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public static JSONObject xml2JSON(byte[] xml) throws JDOMException, IOException  {
        JSONObject json = new JSONObject();
        InputStream is = new ByteArrayInputStream(xml);
        SAXBuilder sb = new SAXBuilder();
        org.jdom2.Document doc = sb.build(is);
        Element root = doc.getRootElement();
        json.put(root.getName(), iterateElement(root));
        return json;
    }
	private static JSONObject iterateElement(Element element) {
        List node = element.getChildren();
        Element et = null;
        JSONObject obj = new JSONObject();
        List list = null;
        for (int i = 0; i < node.size(); i++) {
            list = new LinkedList();
            et = (Element) node.get(i);
            if (et.getTextTrim().equals("")) {
                if (et.getChildren().size() == 0)
                    continue;
                if (obj.containsKey(et.getName())) {
                    list = (List) obj.get(et.getName());
                }
                list.add(iterateElement(et));
                obj.put(et.getName(), list);
            } else {
                if (obj.containsKey(et.getName())) {
                    list = (List) obj.get(et.getName());
                }
                list.add(et.getTextTrim());
                obj.put(et.getName(), list);
            }
        }
        return obj;
    }
	

}